package com.tgu.home_action.service.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
// 导入 SMS 模块的 client
import com.tencentcloudapi.sms.v20190711.SmsClient;
// 导入要请求接口对应的 request response 类
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * Tencent Cloud Sms Sendsms
 * https://cloud.tencent.com/document/product/382/38778
 */

@Service
public class MessageService {

    @Value("${message.secretId}")
    private String secretId;
    @Value("${message.secretKey}")
    private String secretKey;
    @Value("${message.appId}")
    private String appId;
    @Value("${message.sign}")
    private String sign;

    /**
     * 发送单条短信
     */
    public SendStatus sendMessage(String phone, String templateID, String[] templateParams) throws TencentCloudSDKException {
        Credential cred = new Credential(secretId, secretKey);
        SmsClient client = new SmsClient(cred, "");
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppid(appId);
        req.setSign(sign);
        req.setTemplateID(templateID);
        String[] phoneNumbers = {StringService.phoneAdd86(phone)};
        req.setPhoneNumberSet(phoneNumbers);
        req.setTemplateParamSet(templateParams);
        SendSmsResponse res = client.SendSms(req);
        SendStatus[] sendStatuses = res.getSendStatusSet();
        System.out.println(SendSmsResponse.toJsonString(res));
        System.out.println(res.getRequestId());
        return sendStatuses[0];
    }

}